var app = getApp()
Page({
  data: {
    region: ["北京市", "北京市", "东城区"],
    regionValue: [], //省市
    showRegion: false, //省市
    province_id: 0, //省
    city_id: 0, //市区
    district_id: 0, //乡镇
    province_name: '', //省
    city_name: '', //市区
    district_name: '', //乡镇
    town:'',
    town_id:'',
  },
  //获取省市
  chooseRegion: function() {
    this.setData({
      showRegion: true,
    });
  },
  //获取省市
  emitHideRegion: function(e) {
    console.log('h获取地址',e)
    this.setData({
      showRegion: e.detail.showRegion,
      regionValue: e.detail.regionValue,
      province_id: e.detail.regionValue[0].id,
      city_id: e.detail.regionValue[1].id,
      district_id: e.detail.regionValue[2].id,
      province_name: e.detail.regionValue[0].name,
      city_name: e.detail.regionValue[1].name,
      district_name: e.detail.regionValue[2].name,
      town:e.detail.regionValue[3].name,
      town_id:e.detail.regionValue[3].id
    });

    console.log("省市" + this.data.province_id + this.data.city_id + this.data.district_id)
  },
  onLoad: function(e) {
    var that = this;
    that.GetAddress()
  },
  chooseLocation: function() {
    var that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log(res);
        that.setData({
          curChooseAddress: res.name ? res.name : res.address,
          curChooseLatitude: res.latitude,
          curChooseLongitude: res.longitude
        })
      },
      fail: (res) => {
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
              that.setData({
                isShowsetting: true
              })
            } else {
              that.setData({
                locationTips: "无法获取您的当前定位地址，请打开定位权限"
              })
            }
          }
        })
      }
    })
  },
  hideSetting: function() {
    var that = this;
    that.setData({
      isShowsetting: false
    })
  },
  openSetting: function() {
    var that = this;
    that.hideSetting();
    wx.openSetting({
      success: (res) => {
        res.authSetting = {
          "scope.userLocation": true
        }
      },
      fail: function(res) {
        console.log(res);
      }
    })
  },
  inputChange: function(e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]: e.detail.value
    })
  },
  saveAddress: function() {
    var that = this;
    var region = that.data.region;
    var data = {};
    data.map = 'applet_address_add';
    data.name = that.data.name ? that.data.name : '';
    data.mobile = that.data.mobile ? that.data.mobile : '';
    data.pro = that.data.province_name ? that.data.province_name : '';
    data.city = that.data.city_name ? that.data.city_name : '';
    data.area = that.data.district_name ? that.data.district_name : '';;
    data.pcda = that.data.curChooseAddress ? that.data.curChooseAddress : '';
    data.lat = that.data.curChooseLatitude ? that.data.curChooseLatitude : '';
    data.lng = that.data.curChooseLongitude ? that.data.curChooseLongitude : '';
    //省市
    data.province_id = that.data.province_id ? that.data.province_id : '';
    data.city_id = that.data.city_id ? that.data.city_id : '';
    data.district_id = that.data.district_id ? that.data.district_id : '';
    data.address = that.data.addressdetail ? that.data.addressdetail : '';
    data.code = that.data.code ? that.data.code : '';
    data.town = that.data.town?that.data.town:'';
    data.town_id = that.data.town_id?that.data.town_id:'';
    if (!data.name) {
      app.errorTip(that, '请输入姓名', 2000);
      return;
    }
    if (!data.mobile) {
      app.errorTip(that, '请输入正确的手机号', 2000);
      return;
    }
    if(!data.province_id||!data.city_id||!data.district_id){
      app.errorTip(that, '请选择配送区域', 2000);
      return;
    }
    // if (!data.pcda) {
    //   app.errorTip(that, '请选择地址信息', 2000);
    //   return;
    // }
    // if (!data.pro || !data.city || !data.area) {
    //   app.errorTip(that, '请选择省市区', 2000);
    //   return;
    // }
    if (!data.address) {
      app.errorTip(that, '请输入详细地址', 2000);
      return;
    }
    console.log("获取data"+JSON.stringify(data));
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function(res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.showToast({
            title: res.data.data.msg,
            icon: 'success',
            duration: 2000,
            success: function() {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.em,
            showCancel: false
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  GetAddress() {//获取地址
    let that = this
    let data = {
      map:'applet_address_get_by_pid',
      pid: 0
    }
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      method:'GET',
      success: function(res) {
        that.setData({
          area:res.data.data
        })
      }
    })
  }
})